1
|
|
|
/** global: UB */ |
2
|
|
|
|
3
|
|
|
var stringFuncs = { |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
// checks |
7
|
|
|
isAllCaps: function(){ |
8
|
|
|
var str = this; |
9
|
|
|
return (str.toUpperCase() == str); |
10
|
|
|
}, |
11
|
|
|
isUppercase: function(){ |
12
|
|
|
var str = this; |
13
|
|
|
return (str.toUpperCase() == str); |
14
|
|
|
}, |
15
|
|
|
isLowercase: function(){ |
16
|
|
|
var str = this; |
17
|
|
|
return (str.toLowerCase() == str); |
18
|
|
|
}, |
19
|
|
|
isComplexNumber: function(){ |
20
|
|
|
var text = this.toString(); |
21
|
|
|
return text.contains("0x") || text.contains("0X") || text.contains("+e") || text.contains("+E") || text.contains("-e") || text.contains("-E"); |
22
|
|
|
}, |
23
|
|
|
|
24
|
|
|
isBlank: function(trimAndCheck = true){ |
25
|
|
|
var str = this.toString(); |
26
|
|
|
if (str == "") { |
27
|
|
|
return true; |
28
|
|
|
} |
29
|
|
|
if (trimAndCheck) { |
30
|
|
|
return str.trim() == ""; |
31
|
|
|
} |
32
|
|
|
return false; |
33
|
|
|
}, |
34
|
|
|
isEmpty: function(trimAndCheck = true){ |
35
|
|
|
var str = this.toString(); |
36
|
|
|
return str.isBlank(trimAndCheck); |
37
|
|
|
}, |
38
|
|
|
isWhitespace: function(newlines){ |
39
|
|
|
var char = this; |
40
|
|
|
if (newlines) { |
41
|
|
|
return char == ' ' || char == '\r' || char == '\n' || char == '\t'; |
42
|
|
|
} |
43
|
|
|
return char == ' ' || char == '\t'; |
44
|
|
|
}, |
45
|
|
|
isNumber: function(){ |
46
|
|
|
var char = this; |
47
|
|
|
var code = char.charCodeAt(0); |
48
|
|
|
return (code >= UB.charCodes._0 && code <= UB.charCodes._9); |
49
|
|
|
}, |
50
|
|
|
isAlphaNumeric: function(){ |
51
|
|
|
var char = this; |
52
|
|
|
var code = char.charCodeAt(0); |
53
|
|
|
if (code >= 48 && code <= 57){ |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
if (code >= 65 && code <= 90){ |
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
if (code >= 97 && code <= 122){ |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
return false; |
63
|
|
|
}, |
64
|
|
|
isAlphabet: function(){ |
65
|
|
|
return this.isLetter(); |
66
|
|
|
}, |
67
|
|
|
isLetter: function(){ |
68
|
|
|
var char = this; |
69
|
|
|
var code = char.charCodeAt(0); |
70
|
|
|
if (code >= 65 && code <= 90){ |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
if (code >= 97 && code <= 122){ |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
return false; |
77
|
|
|
}, |
78
|
|
|
isQuote: function(){ |
79
|
|
|
var char = this; |
80
|
|
|
return (char == '"' || char == "'"); |
81
|
|
|
}, |
82
|
|
|
isMultiline: function(trimAndCheck = true){ |
83
|
|
|
var str = this.toString(); |
84
|
|
|
|
85
|
|
|
// if the trimmed string has newline chars then is multiline |
86
|
|
|
if (trimAndCheck) { |
87
|
|
|
str = str.replace(UB.regex.Trim, ""); |
88
|
|
|
} |
89
|
|
|
return (str.indexOf("\n") > -1 || str.indexOf("\r") > -1); |
90
|
|
|
}, |
91
|
|
|
isNewline: function(){ |
92
|
|
|
var str = this; |
93
|
|
|
|
94
|
|
|
// if the trimmed string has newline chars then is multiline |
95
|
|
|
return str == "\n" || str == "\r"; |
96
|
|
|
}, |
97
|
|
|
isSingleline: function(){ |
98
|
|
|
var str = this.toString(); |
99
|
|
|
|
100
|
|
|
// if the trimmed string has no newline chars then is singleline |
101
|
|
|
return !str.isMultiline(); |
102
|
|
|
}, |
103
|
|
|
isDigit: function(){ |
104
|
|
|
var ch = this; |
105
|
|
|
return ( ch >= '0' && ch <= '9' ); |
106
|
|
|
}, |
107
|
|
|
isHexDigit: function(){ |
108
|
|
|
var ch = this; |
109
|
|
|
return ( (ch >= '0' && ch <= '9') || ( ch >= 'A' && ch <= 'F' ) || ( ch >= 'a' && ch <= 'f' ) ); |
110
|
|
|
}, |
111
|
|
|
isAny: function(haystacks, doTrim = true, caseSensitive = true){ |
112
|
|
|
var needle = this.toString(); |
113
|
|
|
|
114
|
|
|
// return true if any haystack equals the needle |
115
|
|
|
for (var s = 0, sl = haystacks.length;s<sl;s++){ |
116
|
|
|
var str = haystacks[s]; |
117
|
|
|
if (doTrim) { |
118
|
|
|
str = str.trim(); |
119
|
|
|
} |
120
|
|
|
if (caseSensitive) { |
121
|
|
|
if (needle == str) { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
}else { |
125
|
|
|
if (needle.isEqual(str, false)) { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
return false; |
131
|
|
|
}, |
132
|
|
|
isNumeric: function(withDot = true, withDash = true){ |
133
|
|
|
var str = this; |
134
|
|
|
|
135
|
|
|
// per char |
136
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
137
|
|
|
var code = str.charCodeAt(c); |
138
|
|
|
|
139
|
|
|
// if char not number ... or dot and dot not ok ... or dash and dash not ok |
140
|
|
|
if (!((code >= UB.charCodes._0 && code <= UB.charCodes._9) || (withDot && code == UB.charCodes.Dot) || (withDash && code == UB.charCodes.Minus && c === 0))) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// if all chars OK then is numeric |
146
|
|
|
return true; |
147
|
|
|
}, |
148
|
|
|
isInt: function(){ |
149
|
|
|
var text = this; |
150
|
|
|
return String(parseInt(text)) == text; |
151
|
|
|
}, |
152
|
|
|
isWindowsPath: function(){ |
153
|
|
|
var str = this; |
154
|
|
|
|
155
|
|
|
/// c:\temp |
156
|
|
|
/// D:\My\Data\File.txt |
157
|
|
|
|
158
|
|
|
if (str.charAt(1) == ":"){ |
159
|
|
|
if (str.indexOf("\\") == 2) { |
160
|
|
|
if(str.charAt(0).isAlphaNumeric()){ |
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
return false; |
166
|
|
|
}, |
167
|
|
|
isWindowsPathAll: function(){ |
168
|
|
|
var str = this; |
169
|
|
|
|
170
|
|
|
/* |
171
|
|
|
* WORKS FOR: |
172
|
|
|
* |
173
|
|
|
c:\temp |
174
|
|
|
D:\directoryname\testing\ |
175
|
|
|
\john-desktop\tempdir\ |
176
|
|
|
*/ |
177
|
|
|
|
178
|
|
|
var c0 = str.charAt(0); |
179
|
|
|
var c1 = str.charAt(1); |
180
|
|
|
var c2 = str.charAt(2); |
181
|
|
|
if(c0 != "\\" || c1 != "\\"){ |
182
|
|
|
if(c0.match(new RegExp("^[a-zA-Z]"))){ |
183
|
|
|
if (c1.match(new RegExp("^[:]"))){ |
184
|
|
|
if (c2.match(new RegExp("^[\\/\\\\]"))) { |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
return false; |
191
|
|
|
}, |
192
|
|
|
isURL: function(){ |
193
|
|
|
var str = this; |
194
|
|
|
if (str.indexOf("/") > -1) { |
195
|
|
|
return UB.regex.IsURL.test(str); |
196
|
|
|
} |
197
|
|
|
return false; |
198
|
|
|
}, |
199
|
|
|
isEmail: function(){ |
200
|
|
|
var str = this; |
201
|
|
|
|
202
|
|
|
// perform quick tests before regex |
203
|
|
|
var lastAtPos = str.lastIndexOf('@'); |
204
|
|
|
if (lastAtPos === -1) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
var lastDotPos = str.lastIndexOf('.'); |
208
|
|
|
if (lastDotPos === -1) { |
209
|
|
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// Last "@" before last "." since "@" cannot be part of server name |
213
|
|
|
// something (the email username) before the last @ |
214
|
|
|
// no "@@" in the address. Even if "@" appears as the last character in email username, it has to be quoted so " would be between that "@" and the last "@" in the address |
215
|
|
|
// at least 3 characters before the last dot, for example "[email protected]" |
216
|
|
|
// enough characters after the last dot to form a two-character domain |
217
|
|
|
if ((lastAtPos < lastDotPos && lastAtPos > 0 && str.indexOf('@@') === -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2) == false) { |
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// perform slow regex test |
222
|
|
|
/// http://stackoverflow.com/a/46181 |
223
|
|
|
return UB.regex.IsEmail.test(str); |
224
|
|
|
}, |
225
|
|
|
isIPAddress: function(){ |
226
|
|
|
var str = this; |
227
|
|
|
if (str.indexOf(".") > -1) { |
228
|
|
|
return UB.regex.IsIP.test(str); |
229
|
|
|
} |
230
|
|
|
return false; |
231
|
|
|
}, |
232
|
|
|
isSymbol: function(){ |
233
|
|
|
var char = this; |
234
|
|
|
return UB.commonSymbols.indexOf(char) > -1; |
235
|
|
|
}, |
236
|
|
|
isVariable: function(spaceOk = false){ |
237
|
|
|
var str = this; |
238
|
|
|
|
239
|
|
|
// if first char is LETTER.. |
240
|
|
|
if (str.length > 0 && str.charAt(0).isOfCharRange("letter")) { |
241
|
|
|
|
242
|
|
|
// ..and other chars are LETTER, DIGIT or "_" |
243
|
|
|
for (var c = 1, cl = str.length;c<cl;c++){ |
244
|
|
|
var char = str.charAt(c); |
245
|
|
|
if (char.isDigit() || char.isLetter() || char == "_" || (spaceOk && char == " ")) { |
|
|
|
|
246
|
|
|
}else { |
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
return true; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return false; |
254
|
|
|
}, |
255
|
|
|
|
256
|
|
|
// counting |
257
|
|
|
countStartingChars: function(char){ |
258
|
|
|
var str = this; |
259
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
260
|
|
|
if (str.charAt(c) != char) { |
261
|
|
|
return c; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
return cl; |
265
|
|
|
}, |
266
|
|
|
countEndingChars: function(char){ |
267
|
|
|
var str = this; |
268
|
|
|
var cl = str.length; |
269
|
|
|
for (var c = cl;c >= 0;c--){ |
270
|
|
|
if (str.charAt(c) != char) { |
271
|
|
|
return cl - c; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
return cl; |
275
|
|
|
}, |
276
|
|
|
countStartingUppercase: function(){ |
277
|
|
|
var str = this; |
278
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
279
|
|
|
if (!str.charAt(c).isUppercase()) { |
280
|
|
|
return c; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
return 0; |
284
|
|
|
}, |
285
|
|
|
countStartingLowercase: function(){ |
286
|
|
|
var str = this; |
287
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
288
|
|
|
if (!str.charAt(c).isLowercase()) { |
289
|
|
|
return c; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
return 0; |
293
|
|
|
}, |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
none:null |
297
|
|
|
}; |
298
|
|
|
|
299
|
|
|
// register funcs |
300
|
|
|
UB.registerFuncs(String.prototype, stringFuncs); |
301
|
|
|
|